<template>
  <div class="navbar">
    <hamburger
      id="hamburger-container"
      :is-active="sidebar.opened"
      class="hamburger-container"
      @toggleClick="toggleSideBar"
    />

    <breadcrumb
      id="breadcrumb-container"
      class="breadcrumb-container"
      v-if="!topNav"
    />
    <top-nav id="topmenu-container" class="topmenu-container" v-if="topNav"/>

    <div class="right-menu flex">
      <template v-if="device !== 'mobile'">
        <span style="color: #11a983; vertical-align: text-bottom"
        ><el-tag type="primary"
        >{{ $store.getters.name }}&nbsp;&nbsp;{{
            roles[$store.getters.curRole]
          }}</el-tag
        ></span
        >
        <el-tooltip class="item" effect="dark" placement="bottom">
          <div
            class="icon shou rela"
            @click="openMyNotice"
            v-if="myNotice.length > 0"
          >
            <div class="redPoint absolute"></div>
            <img
              class="not_systemIcon"
              src="@/assets/images/gonggao.png"
              alt="加载失败"
            />
            <img
              class="not_systemIcon_active"
              src="@/assets/images/gonggao.png"
              alt="加载失败"
            />
          </div>
          <div slot="content">公告</div>
        </el-tooltip>
        <el-tooltip class="item" effect="dark" placement="bottom">
          <div class="icon shou rela" @click="openMsg">
            <div class="redPoint absolute" v-if="total > 0"></div>
            <img
              class="not_systemIcon"
              src="@/assets/images/classify/not_systemIcon2.svg"
              alt="加载失败"
            />
            <img
              class="not_systemIcon_active"
              src="@/assets/images/classify/not_systemIcon2_active.svg"
              alt="加载失败"
            />
          </div>
          <div slot="content">消息中心</div>
        </el-tooltip>
        <search id="header-search" class="right-menu-item"/>

        <!--        <el-tooltip content="源码地址" effect="dark" placement="bottom">-->
        <!--          <ruo-yi-git id="ruoyi-git" class="right-menu-item hover-effect" />-->
        <!--        </el-tooltip>-->

        <!--        <el-tooltip content="文档地址" effect="dark" placement="bottom">-->
        <!--          <ruo-yi-doc id="ruoyi-doc" class="right-menu-item hover-effect" />-->
        <!--        </el-tooltip>-->

        <screenfull id="screenfull" class="right-menu-item hover-effect"/>

        <el-tooltip content="布局大小" effect="dark" placement="bottom">
          <size-select id="size-select" class="right-menu-item hover-effect"/>
        </el-tooltip>
      </template>

      <el-dropdown
        class="avatar-container right-menu-item hover-effect"
        trigger="click"
      >
        <div class="avatar-wrapper">
          <img
            v-if="!avatar"
            src="@/assets/images/head_img.png"
            class="user-avatar"
          />
          <img v-if="avatar" :src="avatar" class="user-avatar"/>
          <i class="el-icon-caret-bottom"/>
        </div>
        <el-dropdown-menu slot="dropdown">
          <router-link to="/user/profile">
            <el-dropdown-item>
              <span>个人中心</span>
            </el-dropdown-item>
          </router-link>
          <router-link to="/user/resetPwd">
            <el-dropdown-item divided>
              <span> 修改密码 </span>
            </el-dropdown-item>
          </router-link>

          <!--          <el-dropdown-item @click.native="setting = true">-->
          <!--            <span>布局设置</span>-->
          <!--          </el-dropdown-item>-->
          <el-dropdown-item
            divided
            @click.native="changeRole"
            v-if="$store.getters.roleList.length > 1"
          >
            <span>切换身份</span>
          </el-dropdown-item>
          <el-dropdown-item divided @click.native="logout">
            <span>退出登录</span>
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
    <changeRoleDia
      :open.sync="open"
      :roles="$store.getters.roles"
      type="replace"
    ></changeRoleDia>
    <notification
      @close="closeReturn"
      type="admin"
      :open.sync="notification_open"
      :arr="notification_arr"
    >
    </notification>
  </div>
</template>

<script>
import changeRoleDia from '@/components/changeRoleDia'
import notification from '@/components/notification'
import { mapGetters } from 'vuex'
import Breadcrumb from '@/components/Breadcrumb'
import TopNav from '@/components/TopNav'
import Hamburger from '@/components/Hamburger'
import Screenfull from '@/components/Screenfull'
import SizeSelect from '@/components/SizeSelect'
import Search from '@/components/HeaderSearch'
import {
  getNotice,
  myAnnouncement,
  changeReadStatus
} from '@/api/notificationCenter/notification'

export default {
  components: {
    changeRoleDia,
    Breadcrumb,
    TopNav,
    Hamburger,
    Screenfull,
    SizeSelect,
    Search,
    notification
  },
  props: {
    msgUnreadTotal: {
      type: [String, Number],
      default: 0
    }
  },
  data() {
    return {
      myNotice: [],
      notification_open: false,
      notification_arr: [],
      total: 0,
      open: false,
      roles: {
        student: '学生',
        teacher: '教师',
        schoolAdmin: '学校管理员',
        admin: '超级管理员',
        system: '系统管理员',
        finance: '财务',
        branchAdmin: '分院管理员'
      }
    }
  },
  mounted() {
    this.getMyAnnouncement()
  },
  computed: {
    ...mapGetters(['sidebar', 'avatar', 'device']),
    setting: {
      get() {
        return this.$store.state.settings.showSettings
      },
      set(val) {
        this.$store.dispatch('settings/changeSetting', {
          key: 'showSettings',
          value: val
        })
      }
    },
    topNav: {
      get() {
        return this.$store.state.settings.topNav
      }
    }
  },
  watch: {
    msgUnreadTotal(val) {
      this.total = val
    }
  },
  methods: {
    async openMyNotice() {
      await changeReadStatus(this.myNotice[0].noticeId)
      let id = this.myNotice[0].noticeId
      this.myNotice = []
      this.$router.push({
        path: '/announcement/previewAnnouncementManagement',
        query: {
          type: 'detail',
          id: id
        }
      })
    },
    async getMyAnnouncement() {
      let res = await myAnnouncement()
      if (res.data) {
        this.myNotice = [res.data]
      }
    },
    closeReturn(e) {
      console.log('closeReturn', e)
      this.total = e
    },
    async openMsg() {
      // 获取通知
      let res = await getNotice()
      res.data.map((item) => {
        item.noticeFiles = JSON.parse(item.noticeFiles)
      })
      this.notification_arr = res.data
      this.notification_open = true
    },
    async getList() {
      let res = await getNotice()
      res.data.map((item) => {
        item.noticeFiles = JSON.parse(item.noticeFiles)
      })
      this.notification_arr = res.data
      const hasUnreadNotice = res.data.some(
        (notice) => notice.readStatus === false
      )
      this.total = hasUnreadNotice ? 1 : 0
    },
    changeRole() {
      this.open = true
    },
    toggleSideBar() {
      this.$store.dispatch('app/toggleSideBar')
    },
    async logout() {
      this.$confirm('确定注销并退出系统吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$store.dispatch('LogOut').then(() => {
            location.href = `${process.env.VUE_APP_BASE_PATH}index`
            // this.$router.replace({
            //   path: `${process.env.VUE_APP_BASE_PATH}index`,
            // });
          })
        })
        .catch(() => {
        })
    }
  }
}
</script>

<style lang="scss" scoped>
.icon {
  .redPoint {
    background: #ff3b30;
    width: 8px;
    height: 8px;
    border-radius: 4px;
    top: 0;
    right: 0;
  }

  margin: auto 5px auto 10px;
  width: 24px;
  height: 24px;

  .not_systemIcon {
    display: block;
  }

  .not_systemIcon_active {
    display: none;
  }

  img {
    width: 24px;
    height: 24px;
  }
}

.navbar {
  height: 50px;
  overflow: hidden;
  position: relative;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);

  .hamburger-container {
    line-height: 46px;
    height: 100%;
    float: left;
    cursor: pointer;
    transition: background 0.3s;
    -webkit-tap-highlight-color: transparent;

    &:hover {
      background: rgba(0, 0, 0, 0.025);
    }
  }

  .breadcrumb-container {
    float: left;
  }

  .topmenu-container {
    position: absolute;
    left: 50px;
  }

  .errLog-container {
    display: inline-block;
    vertical-align: top;
  }

  .right-menu {
    float: right;
    height: 100%;
    line-height: 50px;

    &:focus {
      outline: none;
    }

    .right-menu-item {
      display: inline-block;
      padding: 0 8px;
      height: 100%;
      font-size: 18px;
      color: #5a5e66;
      vertical-align: text-bottom;

      &.hover-effect {
        cursor: pointer;
        transition: background 0.3s;

        &:hover {
          background: rgba(0, 0, 0, 0.025);
        }
      }
    }

    .avatar-container {
      margin-right: 30px;

      .avatar-wrapper {
        margin-top: 5px;
        position: relative;

        .user-avatar {
          cursor: pointer;
          width: 40px;
          height: 40px;
          border-radius: 10px;
        }

        .el-icon-caret-bottom {
          cursor: pointer;
          position: absolute;
          right: -20px;
          top: 25px;
          font-size: 12px;
        }
      }
    }
  }
}
</style>
